#include "./status_task.h"
#include "../driver/cmd_uart.h"
#include "../driver/ctrl.h"
#include "../driver/sensor.h"
#include "../driver/nozzle.h"
#include "../driver/door.h"
#include "./draw_task.h"
#include "./steam_heat_task.h"
#include "./water_heat_task.h"

namespace task {

StatusTask statusTask;

using namespace driver;

constexpr uint16_t APP_VERSION = 0x06;

void StatusTask::run() {
    uint8_t seq = 0;
    for (;;) {
        uint16_t sensor_status = sensor.is_water_level_high() |
            (sensor.is_water_level_low() << 1) |
            (sensor.is_go_switch() << 2) |
            (sensor.is_leak1() << 3) |
            (sensor.is_leak2() << 4) |
            (sensor.is_bowl_checked() << 5) |
            (nozzle.is_alm() << 6) |
            (nozzle.is_origin() << 7) |
            (nozzle.is_origin2() << 8) |
            (door.is_close() << 9) |
            (door.is_lock() << 10);

        sensor.read_temp();
        cmdUart.notify(
            seq, conn::proto::CMD_STATUS,
            APP_VERSION, ctrl.flags(),
            sensor.flow_count(), sensor.water_temp(),
            sensor.steam_temp(), sensor.kpa(), sensor_status,
            drawTask.state(), waterHeatTask.state(), steamHeatTask.state()
        );
        seq += 1;
        osDelay(450);
    }
}

}

